package com.vic.mvc.framework.orm.load;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.vic.mvc.framework.orm.annotation.ColumnVic;
import com.vic.mvc.framework.orm.annotation.EntityVic;
import com.vic.mvc.framework.orm.annotation.TableVic;
import com.vic.mvc.framework.orm.bean.FieldAnnotationBean;
import com.vic.mvc.framework.tools.ClassTool;
import com.vic.mvc.framework.util.ArrayUtil;
import com.vic.mvc.framework.util.ObjectUtil;
import com.vic.mvc.framework.util.PrintUtil;
import com.vic.mvc.framework.util.StringUtil;


public class EntityLoad {

	//entity class -- table
	private static Map<Class<?>, String> entityTableMap = new HashMap<Class<?>, String>();
	//entity class -- ( field - FieldAnnotationBean)
	private static Map<Class<?>, Map<String, FieldAnnotationBean>> entityFieldMap = new HashMap<Class<?>, Map<String, FieldAnnotationBean>>();
	
	
	static {
		List<Class<?>> entityClassList = ClassTool.getClassByAnnotation(EntityVic.class);
		for(Class<?> entityClass : entityClassList) {
			
			//表名
			if(entityClass.isAnnotationPresent(TableVic.class)) {//注解了@TableVic
				String  tableName = entityClass.getAnnotation(TableVic.class).tableName();
				if(StringUtil.isEmpty(tableName)) {
					entityTableMap.put(entityClass, StringUtil.toUnderLine(entityClass.getSimpleName()));
				}else {
					entityTableMap.put(entityClass, tableName);
				}
			}else {
				entityTableMap.put(entityClass, StringUtil.toUnderLine(entityClass.getSimpleName()));
			}
			
			//字段
			Map<String,FieldAnnotationBean> fieldMap = new HashMap<String,FieldAnnotationBean>();
			//entity self
			Field[] fields = entityClass.getDeclaredFields();
			if(ArrayUtil.isNotEmpty(fields)) {
				for(Field field : fields) {
					String fieldName = field.getName();
					FieldAnnotationBean fb = null;
					if(field.isAnnotationPresent(ColumnVic.class)){//只有加注解的才使用
						ColumnVic columnVic = field.getAnnotation(ColumnVic.class);
						String columnName = columnVic.name();
						if(StringUtil.isEmpty(columnName)) {
							columnName = StringUtil.toUnderLine(fieldName);//如果未加name属性 则用field 驼峰转下划线
						}
						fb = new FieldAnnotationBean(columnName, columnVic.nullable(), columnVic.length(), 
								columnVic.precision(), columnVic.scale());
						fieldMap.put(fieldName, fb);
					}
				}
			}
			
			//entity super
			Class<?> superClass = entityClass.getSuperclass();
			if(ObjectUtil.isNotNull(superClass) && superClass != Object.class){
				Field[] superfields = entityClass.getDeclaredFields();
				if(ArrayUtil.isNotEmpty(superfields)) {
					for(Field field : superfields) {
						String superFieldName = field.getName();
						FieldAnnotationBean fb = null;
						if(field.isAnnotationPresent(ColumnVic.class)){//只有加注解的才使用
							ColumnVic columnVic = field.getAnnotation(ColumnVic.class);
							String columnName = columnVic.name();
							if(StringUtil.isEmpty(columnName)) {
								columnName = StringUtil.toUnderLine(superFieldName);//如果未加name属性 则用field 驼峰转下划线
							}
							fb = new FieldAnnotationBean(columnName, columnVic.nullable(), columnVic.length(), 
									columnVic.precision(), columnVic.scale());
							
							if(ObjectUtil.isNull(fieldMap.get(superFieldName))){//子类中不存在的field 才放进去
								fieldMap.put(superFieldName, fb);
							}
							
						}
					}
				}
			}
			entityFieldMap.put(entityClass, fieldMap);
		}
		System.err.printf("实体Entity初始化完成-数量:    %d" ,entityTableMap.size());
		System.out.println();
		
	}
	
	public static void init(){
		//这个是用来测试打印的 可以去掉，真实的作用是加载这个类，运行其中的static{}
		System.out.println("初始化加载的entity Table");
		PrintUtil.printMap(entityTableMap);
		System.out.println("初始化加载的entity Field:");
		for(Map.Entry<Class<?>, Map<String, FieldAnnotationBean>> entry :entityFieldMap.entrySet()){
			Class<?> key = entry.getKey();
			System.out.println("\t类名=" + key.getName());
			Map<String, FieldAnnotationBean> value = entry.getValue();
			if(value != null) {
				System.out.println("\t\t{");
				for(Map.Entry<String, FieldAnnotationBean> entry2 : value.entrySet()) {
					System.out.println("\t\t\t属性 = " + entry2.getKey());
					System.out.print(" \t\t\t列注解属性 ");
							System.out.println(value==null?"":value.toString());
				}
				System.out.println("\t\t}");
			}
					
			
		}
	}
}
